/**
 * Created by jt on 2017/5/6.
 */
import "./index.less";
import "../../../unit/common/js/bootstrap_js/jquery.min.js";
import "../../../unit/common/js/bootstrap_js/select2.min.js";
import "../../../unit/common/js/bootstrap_js/jquery.multi-select.js";
import "../../../unit/common/js/bootstrap_js/jquery.dataTables.js";
import "../../../unit/common/js/bootstrap_js/form-samples.js";
import "../../../unit/common/js/bootstrap_js/table-editable.js";
import "../../../unit/common/js/bootstrap_js/form-components.js";

jQuery(document).ready(function() {
    App.init();
    // FormComponents.init();
    var token = sessionStorage.getItem('token');
    if (token == null || token =='') {
        location.href = HOST_FRT+'/logins/login.html';
    }
    jQuery('.logOut').attr('href','../logins/login.html');
    var artclId = getArgs().id;

    jQuery.ajax({
        url: HOST_ADDR+'/cpmp/artcl/view/'+artclId,
        type: 'get',
        // data: JSON.stringify(data),
        dataType: 'json',
        contentType:"application/json",
        headers: {
            'access_token': token,'login_type':'ums'
        },
        success: function(resp){
            if (!resp.success) {
                alert(resp.msgCod+' '+resp.msgInf);
                return;
            }
            console.log(resp);

            jQuery('.artclTtl').val(resp.data.artclTtl);
            jQuery('.artclId').val(resp.data.artclId);
            jQuery('.artclTypCod').val(resp.data.artclTypCod);
            jQuery('.simditor-body').html(resp.data.artclCtt);

            var atches = resp.data.atches;
            if (atches.length > 0){
                for (var i= 0;i<atches.length;i++) {
                    jQuery('#tab_3').append('<div class="controls atchDiv"><span src="'+atches[i].atchUrl+'">'+atches[i].atchNam+'</span>' +
                        '&emsp;<a class="delete underline" href="javascript:;"><i class="icon-trash"></i> 删除</a></div>');
                }
                jQuery('.delete').click(function () {
                    jQuery(this).parent('div').remove();
                });
            }

            let picFlag = true;
            //新增附件
            jQuery('.addFile').click(function () {
                jQuery('#atchFile').click();

                $("#atchFile").unbind().change(function(){
                    picFlag = false;
                    let formData = new FormData();
                    let atchNam=$("#atchFile")[0].files[0].name;
                    let atchUrl;
                    console.log(atchNam);
                    formData.append('file', $("#atchFile")[0].files[0]);

                    // console.log(formData);
                    $.ajax({
                        url: HOST_ADDR+'/cpmp/file/upload/atch',
                        type: 'post',
                        cache: false,
                        data: formData,
                        processData: false,
                        contentType: false,
                        success: function(resp){
                            if(resp.success){
                                // alert('上传文件成功');
                                console.log(resp);
                                atchUrl=resp.data;
                                picFlag = true;
                                jQuery('#tab_3').append('<div class="controls atchDiv"><span src="'+atchUrl+'">'+atchNam+'</span>' +
                                    '&emsp;<a class="delete underline" href="javascript:;"><i class="icon-trash"></i> 删除</a></div>');
                                jQuery('.delete').click(function () {
                                    jQuery(this).parent('div').remove();
                                });

                            }else{
                                alert(resp.msgInf);
                            }
                        },
                        error:function(err){
                            alert('上传文件失败！');
                        }

                    });
                });

            });

            //保存
            jQuery('.subKeep').click(function () {
                let artclTtl = jQuery('.artclTtl').val();
                let artclId = jQuery('.artclId').val();
                let artclTypCod = jQuery('.artclTypCod').val();
                let artclCtt = editor.getValue();
                let atchDtos = [];
                let atchDiv = jQuery('.atchDiv');
                if (atchDiv.length > 0){
                    for (var i= 0;i<atchDiv.length;i++) {
                        atchDtos.push({atchNam:jQuery(atchDiv[i]).find('span').text(),atchUrl:jQuery(atchDiv[i]).find('span').attr('src')});
                    }
                }

                if (artclTtl == '') {
                    alert('请填写文章标题！');
                    return;
                }
                if (artclTypCod == '') {
                    alert('请选择文章类型！');
                    return;
                }
                if (artclCtt == '') {
                    alert('请填写文章内容！');
                    return;
                }

                let data = {
                    "pubFlg":'0',
                    "artclTtl":artclTtl,
                    "artclId":artclId,
                    "artclTypCod": artclTypCod,
                    "artclCtt": artclCtt,
                    "atchDtos": atchDtos
                };

                console.log(data);

                jQuery.ajax({
                    url: HOST_ADDR+'/cpmp/artcl/edit',
                    type: 'post',
                    data: JSON.stringify(data),
                    dataType: 'json',
                    contentType:"application/json",
                    headers: {
                        'access_token': token,'login_type':'ums'
                    },
                    success: function(resp){
                        if (!resp.success) {
                            alert(resp.msgCod+' '+resp.msgInf);
                            return;
                        }
                        console.log(resp);
                        // //页面跳转到index
                        alert('保存成功');
                        location.href = 'article_list.html';

                    },
                    error:function(err){
                        console.log(err);
                    }
                });
            });

            //发布
            jQuery('.sharePro').click(function () {
                let artclTtl = jQuery('.artclTtl').val();
                let artclId = jQuery('.artclId').val();
                let artclTypCod = jQuery('.artclTypCod').val();
                let artclCtt = editor.getValue();
                let atchDtos = [];
                let atchDiv = jQuery('.atchDiv');
                if (atchDiv.length > 0){
                    for (var i= 0;i<atchDiv.length;i++) {
                        atchDtos.push({atchNam:jQuery(atchDiv[i]).find('span').text(),atchUrl:jQuery(atchDiv[i]).find('span').attr('src')});
                    }
                }

                if (artclTtl == '') {
                    alert('请填写文章标题！');
                    return;
                }
                if (artclTypCod == '') {
                    alert('请选择文章类型！');
                    return;
                }
                if (artclCtt == '') {
                    alert('请填写文章内容！');
                    return;
                }

                let data = {
                    "pubFlg":'1',
                    "artclTtl":artclTtl,
                    "artclId":artclId,
                    "artclTypCod": artclTypCod,
                    "artclCtt": artclCtt,
                    "atchDtos": atchDtos
                };

                console.log(data);

                jQuery.ajax({
                    url: HOST_ADDR+'/cpmp/artcl/edit',
                    type: 'post',
                    data: JSON.stringify(data),
                    dataType: 'json',
                    contentType:"application/json",
                    headers: {
                        'access_token': token,'login_type':'ums'
                    },
                    success: function(resp){
                        if (!resp.success) {
                            alert(resp.msgCod+' '+resp.msgInf);
                            return;
                        }
                        console.log(resp);
                        // //页面跳转到index
                        alert('发布成功');
                        location.href = 'article_list.html';

                    },
                    error:function(err){
                        console.log(err);
                    }
                });
            });

        },
        error:function(err){
            console.log(err);
        }
    });
    
});